package com.yinhe.music.yhmusic.personal.feedback;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BasePresneterActivity;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.http.StatusCode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 何晓华
 * @date 2017/9/21
 */

public class FeedBackActivity extends BasePresneterActivity implements FeedbackContract.IFeedbackView {
    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.text_num)
    TextView textNum;
    @BindView(R.id.take)
    Button take;
    private final static String MAX_LENGTH = "300";

    FeedbackPresenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        ButterKnife.bind(this);
        setToolbar("反馈");
        setListener();
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new FeedbackPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    private void setListener() {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String content = editText.getText().toString();
                textNum.setText(content.length() + "/" + MAX_LENGTH);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        take.setOnClickListener((v) -> {
            takeFeedBack();
        });
    }

    private void takeFeedBack() {
        String content = editText.getText().toString();
        if (content.length() < 10) {
            Toast.makeText(this, "请输入不少于10个字符", Toast.LENGTH_SHORT).show();
            return;
        }
        mPresenter.sendFeedback(content);
    }

    @Override
    public void setFeedBack(String msg, int code) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        if (code == StatusCode.CHANGE_DATA) {
            finish();
        }
    }
}
